package thefirst.controller;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import thefirst.LessonBoard.model.LessonBoardListVO;
import thefirst.LessonBoard.model.LessonBoardService;
import thefirst.LessonBoard.model.LessonBoardVO;
import thefirst.member.model.MemberSearchVO;
import thefirst.member.model.MemberVO;
import thefirst.member.model.StudentVO;

@Controller
public class LessonBoardController {
	@Resource(name = "lessonBoardServiceImpl")
	private LessonBoardService lessonBoardService;
/**
 * 글 쓰기를 한후
 * showLessonBoardInfoNoHits.do 로 이동하여 자신이 쓴글을 확인한다.
 * 이때 조회수는 올라가지 않는다.
 * @param lbvo
 * @param session
 * @return
 * @throws SQLException
 */
	@RequestMapping("/writeLessonBoard.do")
	public String writeLessonBoard(LessonBoardVO lbvo, HttpSession session)
			throws SQLException {
		lessonBoardService.writeLessonBoard(lbvo);
		return "redirect:/showLessonBoardInfoNoHits.do?lessonno="
				+ lbvo.getLessonno() + "&studentid=" + lbvo.getStudentid();
	}

	/**
	 * 자신이 쓴 글을 확인한다.
	 *  조회수는 올라가지 않는다.
	 * 
	 * @param no
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/showLessonBoardInfoNoHits.do")
	public ModelAndView showLessonBoardInfoNoHits(LessonBoardVO lbvo)
			throws SQLException {
		return new ModelAndView(
				"LessonBoard/board/showLessonBoardInfo.lessonBoard", "lbvo",
				lessonBoardService.showLessonBoardInfoNoHits(lbvo));
	}

	/**
	 * 등록한 총 게시물 리스트를 보여준다.
	 * 
	 * @param pageNo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/lessonBoardSerch.do")
	public ModelAndView lessonBoardList(String pageNo, HttpSession session)
			throws SQLException {
		return new ModelAndView("LessonBoard/lessonBoardList.lessonBoard",
				"lessonList", lessonBoardService.getLessonBoardList(pageNo));
	}

	/**
	 * 글쓰기 페이지로 이동
	 */
	@RequestMapping("/writeLessonPage.do")
	public String writeLessonBoardPage(LessonBoardVO lbvo,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException {
		return "LessonBoard/board/writeLessonPage.lessonBoard";
	}

	/**
	 * 게시물 상세 정보를 본다.
	 * 이때 조회수가 증가한다.
	 * 
	 * @param no
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/showLessonBoardInfo.do")
	public ModelAndView showLessonBoardInfo(LessonBoardVO lvo)
			throws SQLException {
		LessonBoardVO lbvo = lessonBoardService.showLessonInfo(lvo);
		return new ModelAndView(
				"LessonBoard/board/showLessonBoardInfo.lessonBoard", "lbvo",
				lbvo);
	}

	/**
	 * 삭제합니다.
	 * 
	 * @param lvo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/deleteLessonContent.do")
	public ModelAndView deleteLessonContent(LessonBoardVO lvo)
			throws SQLException {
		lessonBoardService.deleteLessonContent(lvo);
		return new ModelAndView("LessonBoard/lessonBoardList.lessonBoard",
				"lessonList", lessonBoardService.getLessonBoardList("1"));
	}

	/**
	 * 글 수정 페이지로 이동
	 * 
	 * @param lvo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/updateLessonPage.do")
	public ModelAndView updateLessonContent(LessonBoardVO lvo)
			throws SQLException {
		LessonBoardVO lbvo = lessonBoardService.showLessonInfo(lvo);
		return new ModelAndView(
				"LessonBoard/board/updateLessonContent.lessonBoard", "lbvo",
				lbvo);
	}

	/**
	 * 글 수정후 자신의 글을 보여준다.
	 * 이때 조회수는 올라가지 않는다.
	 * @param lvo
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/updateLesson.do")
	public String updateLesson(LessonBoardVO lvo) throws SQLException {
		lessonBoardService.updateBoardContent(lvo);
		return "redirect:/showLessonBoardInfoNoHits.do?lessonno="
				+ lvo.getLessonno() + "&studentid=" + lvo.getStudentid();
	}

	/**
	 * 수업을 검색한후 검색을 setAttribute를 설정한후 
	 * 검색 키워드를 유지시켜 페이징을 할때 키워드 값을 유지시킨다.
	 * 
	 * @param request
	 * @param lvo
	 * @param hopesubject
	 * @param grade
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping("/searchLesson.do")
	public ModelAndView searchLesson(HttpServletRequest request,
			HttpServletResponse response,LessonBoardVO lvo, 
			String hopesubject, String grade) throws SQLException {

		if (grade != "") {
			lvo.setGrade(grade);
		} else if (hopesubject != "") {
			lvo.setHopesubject(hopesubject);
		} else if (lvo == null) {
			System.out.println("널~");
		}
		request.setAttribute("grade", grade);
		request.setAttribute("hopesubject", hopesubject);
		System.out.println("수업 : " + hopesubject + " " + "학년 : " + grade);
		return new ModelAndView(
				"LessonBoard/lessonBoardSearchList.lessonBoard", "searchList",
				lessonBoardService.searchLesson(lvo));
	}

}
